 #define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
        dfs(A, B, C, A.size());
    }

    void dfs(vector<int>& x, vector<int>& y, vector<int>& z, int n)
    {
        if (n == 1)
        {
            z.push_back(x.back());
            x.pop_back();
            return;
        }

        dfs(x, z, y, n - 1);
        z.push_back(x.back());
        x.pop_back();

        dfs(y, x, z, n - 1);
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        if (list1 == nullptr) return list2;
        if (list2 == nullptr) return list1;

        if (list1->val <= list2->val)
        {
            list1->next = mergeTwoLists(list1->next, list2);
            return list1;
        }
        else
        {
            list2->next = mergeTwoLists(list1, list2->next);
            return list2;
        }
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        // ListNode* newhead = new ListNode(0);
        // ListNode* prev = newhead;
        // ListNode* cur = head;
        // while(cur)
        // {
        //     ListNode* next = cur->next;
        //     cur->next = prev->next;
        //     prev->next = cur;
        //     cur = next;
        // }

        // prev = newhead->next;
        // delete newhead;

        // return prev;

        if (head == nullptr || head->next == nullptr) return head;

        ListNode* newhead = reverseList(head->next);

        head->next->next = head;
        head->next = nullptr;

        return newhead;
    }
};

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        // if(head == nullptr || head->next == nullptr) return head;

        // ListNode* newhead = new ListNode(0);
        // newhead->next = head;
        // ListNode* prev = newhead, *cur = newhead->next, *next = cur->next, *nnext = next->next;

        // while(cur && next)
        // {
        //     //交换节点
        //     prev->next = next;
        //     next->next = cur;
        //     cur->next = nnext;

        //     //更换指针
        //     prev = cur;
        //     cur = nnext;
        //     if(cur) next = cur->next;
        //     if(next) nnext = next->next;
        // }

        // cur = newhead->next;
        // delete newhead;
        // return cur;

        if (head == nullptr || head->next == nullptr) return head;

        //交个它将后面的链表进行交换
        ListNode* tmp = swapPairs(head->next->next);

        ListNode* ret = head->next;
        head->next->next = head;
        head->next = tmp;

        return ret;
    }
};
